<!--
 @description
 @fileName TopReportChartLine.vue
 @author zengqiongying
 @created 2023/06/04 14:33:44
-->
<template>
  <div :id="chartId" class="echart-box" />
</template>

<script>
import * as echarts from 'echarts'
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'TopReportChartLine',
  props: {
    xAxisData: {
      type: Array,
      default: () => []
    },
    chartData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      newXData: [],
      newChartData: [],
      windowSize: [0, 0],
      chartId: `echart-line-${uuidv4()}`, // // 用UUID生成图表id，防止id重复
      chartMap: null // 图标实例
    }
  },
  watch: {
    windowSize() {
      this.resizeChart()
    },
    xAxisData: {
      handler(n) {
        this.newXData = n
      },
      immediate: true,
      deep: true
    },
    chartData: {
      handler(n) {
        this.newChartData = n
        this.$nextTick(() => {
          this.drawChart()
        })
      },
      immediate: true,
      deep: true
    }
  },
  mounted() {
    this.initChart()
    this.$nextTick(() => {
      this.drawChart()
    })
  },
  methods: {
    resizeChart() {
      this.chartMap && this.chartMap.resize && this.chartMap.resize()
    },
    initChart() {
      this.windowSize = [document.body.clientWidth, document.body.clientHeight]
      window.addEventListener('resize', () => {
        this.windowSize = [document.body.clientWidth, document.body.clientHeight]
        this.resizeChart()
      })
      this.chartMap = echarts.init(document.getElementById(this.chartId))
      window.onresize = () => {
        this.resizeChart()
      }
    },
    drawChart() {
      const option = {
        color: ['#FEB81D'],
        tooltip: {
          show: false,
          trigger: 'axis',
          confine: true,
          backgroundColor: 'rgba(2, 34, 49, 1)',
          borderColor: '#18d8ff', // tip层边框颜色
          textStyle: {
            color: '#ffffff',
            fontFamily: 'PFZH',
            fontSize: 12,
            lineHeight: 26
          },
          axisPointer: {
            type: 'line',
            lineStyle: {
              type: 'solid',
              color: '#FEB81D', // 线条颜色
              width: 1,
              shadowBlur: 16,
              shadowColor: '#ffffff',
              shadowOffsetX: 0,
              shadowOffsetY: 0
            }
          },
          extraCssText: 'z-index: 9999', // 设置层级
          formatter(params) {
            let tipMsg = `${params[0].name}<br>`
            for (const i in params) {
              const item = params[i]
              tipMsg += `销量: <b style="color:${item.color}">${item.data}</b><br>`
            }
            return `<div class="echart-tooltip-content">${tipMsg}</div>`
          }
        },
        legend: {
          data: ['销量'],
          show: false
        },
        grid: {
          left: '0',
          right: '20',
          bottom: '10',
          top: '10',
          containLabel: true,
          show: false
        },
        xAxis: {
          type: 'category',
          data: this.newXData,
          show: false, // 不显示X轴
          boundaryGap: false, // x轴左右留白
          axisLine: {
            show: false, // 不显示坐标轴线
            lineStyle: {
              color: '#18D8FF'
            }
          },
          tooltip: {
            show: true
          },
          axisTick: {
            show: false // x轴刻度隐藏
          },
          axisLabel: {
            show: false // 不显示坐标轴上的文字
          },
          splitLine: {
            show: false // 不显示网格线
          }
        },
        yAxis: {
          type: 'value',
          show: false, // 不显示Y轴
          axisLine: {
            show: false, // 不显示坐标轴线
            lineStyle: {
              color: '#18D8FF'
            },
            axisLabel: {
              show: false // 不显示坐标轴上的文字
            },
            splitLine: {
              show: false // 不显示网格线
            }
          }
        },
        series: [
          {
            data: this.newChartData,
            type: 'line',
            smooth: true
          }
        ]
      }
      this.chartMap.clear()
      this.chartMap.setOption(option)
      // this.chartMap.dispatchAction({
      //   type: 'showTip',
      //   seriesIndex: 0,
      //   dataIndex: 0
      // })
    }
  }
}

</script>
<style lang='scss' scoped>
  .echart-box{
    width: 100%;
    height: 100%;
    position: relative;
  }
</style>
